package ema4;


/**
 * @author Team TA's
 */

//muutettu kaikki mahdolliset start- ja endTimet StringeistÃ¤ inteiksi
import java.util.ArrayList;
import java.util.HashMap;

import ema4.Weekday.Day;

public class WeekReport extends Report {

	public WeekReport(Schedule schedule, HashMap<String, Object> options) {
		super(schedule, options);
	}

	@Override
	public String toString() {

		if (this.options.containsKey("days")){
			ArrayList<Weekday.Day> days = (ArrayList<Day>)this.options.get("days");
			String[][] res = new String[days.size() + 1][8]; // +1 for header row

			res[0][0] = "\t";

			int i, j;
                        for (i = 1, j = 0; j < Event.VALID_START_TIMES.length; i++, j++){
				res[0][i] = Event.VALID_START_TIMES[j] + "\t";
			}
                        res[0][7] = "20";

			i = 1;
			//int koko = days.size();
                        //for (i = 0; i < koko; i++) {
                        for (Day day : days){
				res[i][0] = day/*s.get(i)*/.toString() + "\t";
				i++;
			}

			i = 1;
                        for (Day d : days){
			//for (i = 0; i < koko; i++){
                                //Day d = days.get(i);
				ArrayList<Event> events = this.schedule.getSchedule().get(d);

				if (events == null){
					return null;
				}
				else if (events.size() == 0){
					for (int x = 1; x < 8; x++) {
						res[i][x] = "\t";
					}
				}

				for (Event event : events){
					String entry = "\t"; // if event is null

					if (event.getLocation() != null) {
					  entry = event.getLocation()+"\t";
					}

					if (event.getStartTime() == 8)     { res[i][1] = entry; }
					else if(event.getStartTime() == 10) { res[i][2] = entry; }
					else if(event.getStartTime() == 12) { res[i][3] = entry;	}
					else if(event.getStartTime() == 14) { res[i][4] = entry; }
					else if(event.getStartTime() == 16) { res[i][5] = entry; }
					else if(event.getStartTime() == 18) { res[i][6] = entry; }

					// fill up with empties
					for (int x = 1; x < 8; x++) {
						if (res[i][x] == null){
							res[i][x] = "\t";
						}
					}

				}
				i++;
			}

			String response = "";

			for (j = 0; j < res.length; j++){
				for (int k = 0; k < res[0].length; k++){
					response += res[j][k];
				}
				response += "\n";
			}

			return response;
		}
		return null;
	}

}